import {
  getConversionListForExternal,
  getConversionForExternal,
  postAddLabel,
  postCancelFocus,
  postSetFocus,
  getUserLabels,
} from '../../services/channel/api';

export default {
  namespace: 'conversionDetail',

  state: {
    loading: true,

    conversionDetailData: {
      list: [],
      pagination:{}
    },

    getUserLabelsData: [],
  },

  effects: {
    *fetchGetConversionListForExternal({ payload , callback}, { call, put }) {
      const response = yield call(getConversionListForExternal, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            conversionDetailData: {
              list: response.data.records,
              pagination: {
                current: response.data.current,
                total: response.data.total,
                size: response.data.size,
              }
            }
          },
        });
        if (callback) callback();
      }
    },

    *fetchGetConversionForExternal({ _ , callback}, { call, put }) {
      const response = yield call(getConversionForExternal);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {conversionData: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchPostAddLabel({ payload , callback}, { call, put }) {
      const response = yield call(postAddLabel, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {addLabelRes: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchPostCancelFocus({ payload , callback}, { call, put }) {
      const response = yield call(postCancelFocus, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {cancleFocusRes: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchPostSetFocus({ payload , callback}, { call, put }) {
      const response = yield call(postSetFocus, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {SetFocusRes: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchGetUserLabels({ payload , callback}, { call, put }) {
      const response = yield call(getUserLabels, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {getUserLabelsData: response.data},
        });
        if (callback) callback();
      }
    },

  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};